//
//  MKTreeNode.m
//  MKUI
//
//  Created by sushangjin on 15/6/18.
//  Copyright (c) 2015年 sushangjin. All rights reserved.
//

#import "MKTreeNode.h"

@implementation MKTreeNode
- (id)init{
	if (self=[super init]) {
		_childrenNodes = [[NSMutableArray alloc] init];
	}
	return self;
}
- (NSArray *)childrenNodes{
	return [_childrenNodes copy];
}
- (NSInteger)level{
	NSInteger level = 0;
	if(self.isLeaf){
		level = 1;
	}else{
		for (MKTreeNode *n in _childrenNodes) {
			level = MAX(n.level,level);
		}
	}
	return level;
}
- (NSArray *)allNodes{
	NSMutableArray *nodes = [[NSMutableArray alloc] init];
	[nodes addObject:self];
	if(!self.isLeaf){
		for (MKTreeNode *n in _childrenNodes) {
			[nodes addObjectsFromArray:[n allNodes]];
		}
	}
	return nodes;
}
- (void)addChildNode:(MKTreeNode *)node{
	if(!node)return;
	node.parentNode = self;
	[_childrenNodes addObject:node];
}
- (void)insertChildNode:(MKTreeNode *)node atIndex:(NSInteger)index{
	if(!node)return;
	node.parentNode = self;
	[_childrenNodes insertObject:node atIndex:index];
}
- (void)removeChildNode:(MKTreeNode *)node{
	if(!node)return;
	node.parentNode = nil;
	[_childrenNodes removeObject:node];
}
- (void)removeChildNodeAtIndex:(NSInteger)index{
	if(index>=0&&index<_childrenNodes.count){
		MKTreeNode *node = [_childrenNodes objectAtIndex:index];
		node.parentNode = nil;
		[_childrenNodes removeObjectAtIndex:index];
	}
}
- (MKTreeNode *)childNodeAtIndex:(NSInteger)index{
	MKTreeNode *node;
	if (index>=0&index<_childrenNodes.count) {
		node = [_childrenNodes objectAtIndex:index];
	}
	return node;
}
- (NSInteger)indexOfChildNode:(MKTreeNode *)node{
	NSInteger index = [_childrenNodes indexOfObject:node];
	return index;
}
- (NSArray *)nodePathOfNode:(MKTreeNode *)node{
	if(!node)return nil;
	NSArray *path = [self nodePathOfNode:node passingTest:^BOOL(MKTreeNode *n1, MKTreeNode *n2) {
		return [n1 isEqual:n2];
	}];
	return path;
}
- (NSArray *)nodePathOfNode:(MKTreeNode *)node passingTest:(BOOL(^)(MKTreeNode *n1,MKTreeNode *n2))passingTest{
	if(!node)return nil;
	NSMutableArray *path = [[NSMutableArray alloc] init];
	if(passingTest(self,node)){
		[path addObject:self];
	}else{
		if(!self.isLeaf){
			for (MKTreeNode *n in _childrenNodes) {
				NSArray *subPath = [n nodePathOfNode:node passingTest:passingTest];
				if(subPath.count){
					[path addObject:self];
					[path addObjectsFromArray:subPath];
					break;
				}
			}
		}
	}
	return path;
}
+ (NSArray *)nodePathOfNode:(MKTreeNode *)node rootNodes:(NSArray *)rootNodes{
	if(!node||rootNodes.count==0)return nil;
	NSArray *path = [self nodePathOfNode:node rootNodes:rootNodes passingTest:^BOOL(MKTreeNode *n1, MKTreeNode *n2) {
		return [n1 isEqual:n2];
	}];
	return path;
}
+ (NSArray *)nodePathOfNode:(MKTreeNode *)node rootNodes:(NSArray *)rootNodes passingTest:(BOOL(^)(MKTreeNode *n1,MKTreeNode *n2))passingTest{
	if(!node||rootNodes.count==0)return nil;
	NSMutableArray *path = [[NSMutableArray alloc] init];
	for (MKTreeNode *n in rootNodes) {
		NSArray *subPath = [n nodePathOfNode:node passingTest:passingTest];
		if(subPath.count){
			[path addObjectsFromArray:subPath];
			break;
		}
	}
	return path;
}
@end
